//
//  TimePlanTableViewCell.m
//  FitTime
//
//  Created by Zone on 16/4/1.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "TimePlanTableViewCell.h"

@implementation TimePlanTableViewCell

- (void)setTimer:(TImes *)timer {
    
    _timer = timer;
    
    if (timer.week.length == 21) {
        self.weekLabel.text = @"The alarm clock every day⏰";
        
        self.weekLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:20];
    } else {
        self.weekLabel.text = timer.week;
        
        self.weekLabel.font = [UIFont systemFontOfSize:18 weight:1.5];
    }
    self.timeLabel.text = [NSString stringWithFormat:@"%@ 时 %@ 分 ", timer.hour, timer.minute];
    
    self.timeLabel.font = [UIFont fontWithName:@"Digital-7" size:30];
    
    self.typeLabel.text = timer.type;
    
    self.typeLabel.font = [UIFont systemFontOfSize:17 weight:1.5];
    
    self.typeLabel.textColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
    
    self.clockView.frame = CGRectMake(5, 5, 70, 70);
    
#pragma mark 表针实现
    if (!_minuteHand.center.x) {
        
        _hourHand.frame = CGRectMake(0, 0, _clockView.bounds.size.width / 2 - 15, 4);
        _hourHand.center = _clockView.center;
        
        _minuteHand.frame = CGRectMake(0, 0, _clockView.bounds.size.width / 2 - 10, 2);
        _minuteHand.center = _clockView.center;
        
        int h;
        if (_timer.hour.intValue >= 12) {
            h = _timer.hour.intValue - 12;
        } else {
            h = _timer.hour.intValue;
        }
        
        CGFloat Hwidth = (_clockView.bounds.size.width / 2 - 15) / 2;
        
        CGAffineTransform Hrotate = CGAffineTransformMakeRotation((h - 3) * M_PI / 6);
        
        _hourHand.center = CGPointMake(_clockView.center.x + (cosf((h - 3) * M_PI / 6) * Hwidth) - 5, _clockView.center.y + (sinf((h - 3) * M_PI / 6) * Hwidth) - 5);
        
        [_hourHand setTransform:Hrotate];
        
        CGFloat Mwidth = (_clockView.bounds.size.width / 2 - 10) / 2;
        
        CGAffineTransform Mrotate = CGAffineTransformMakeRotation((_timer.minute.intValue - 15) * M_PI / 30);
        
        _minuteHand.center = CGPointMake(_clockView.center.x + (cosf((_timer.minute.intValue - 15) * M_PI / 30) * Mwidth) - 5, _clockView.center.y + (sinf((_timer.minute.intValue - 15) * M_PI / 30) * Mwidth) - 5);
        
        [_minuteHand setTransform:Mrotate];
    }
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backView = [UIImageView new];
        [self.contentView addSubview:self.backView];
        
        [self.backView.layer setCornerRadius:10 * SCALEHEIGHT];
        self.backView.layer.masksToBounds = YES;
        
        self.clockView = [UIImageView new];
        [self.backView addSubview:self.clockView];
        self.clockView.image = [UIImage imageNamed:@"表盘.png"];
        self.clockView.contentMode = UIViewContentModeScaleAspectFill;
        
#pragma mark 小表盘实现
        
        self.hourHand = [UIView new];
        _hourHand.backgroundColor = [UIColor redColor];
        self.hourHand.alpha = 0.7;
        [self.clockView addSubview:_hourHand];
        
        self.minuteHand = [UIView new];
        self.minuteHand.alpha = 0.7;
        _minuteHand.backgroundColor = [UIColor blackColor];
        [self.clockView addSubview:_minuteHand];
        
        [self.hourHand.layer setCornerRadius:2];
        self.hourHand.layer.masksToBounds = YES;
        [self.minuteHand.layer setCornerRadius:1];
        self.minuteHand.layer.masksToBounds = YES;
        
        self.dian = [UIView new];
        [self.clockView addSubview:self.dian];
        _dian.backgroundColor = [UIColor blackColor];
        [self.dian.layer setCornerRadius:2];
        self.dian.layer.masksToBounds = YES;
        
        self.weekLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
        
        self.timeLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
        
        self.typeLabel = [UILabel labelWithFrame:CGRectZero color:[UIColor clearColor] alignment:NSTextAlignmentCenter];
        
        [self.backView addSubview:self.weekLabel];
        [self.backView addSubview:self.timeLabel];
        [self.backView addSubview:self.typeLabel];
        
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.backView.frame = self.bounds;
    
    self.backView.image = [UIImage imageNamed:@"overlay.png"];
    self.backView.contentMode = UIViewContentModeScaleToFill;
    
#pragma mark 表盘原点
    [self.dian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(4, 4));
        make.centerX.mas_equalTo(self.clockView.mas_centerX);
        make.centerY.mas_equalTo(self.clockView.mas_centerY);
    }];
    
    [self.weekLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(5);
        make.bottom.equalTo(self.timeLabel.mas_top).offset(-5);
        make.left.equalTo(self.clockView.mas_right).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
        make.height.equalTo(self.timeLabel.mas_height);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(self.weekLabel.mas_height);
        make.top.equalTo(self.weekLabel.mas_bottom).offset(5);
        make.left.equalTo(self.clockView.mas_right).offset(5);
        make.right.equalTo (self.typeLabel.mas_left).offset(-5);
        make.bottom.equalTo(self.contentView).offset(-5);
        make.width.equalTo(self.typeLabel.mas_width).multipliedBy(3);
    }];

    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.top.equalTo(self.timeLabel);
        make.bottom.equalTo(self.timeLabel);
        make.left.equalTo(self.timeLabel.mas_right).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
   
    }];
    

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
